package cn.lesile.o2o.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.geometry.Positions;

public class ImageUtil {
	private static String basePath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
	private static final SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyyMMddHHmmss"); 
	private static 	final Random r=new Random();
	public static String generateThumbnail(CommonsMultipartFile thumbnail, String targetAddr) {
			String realFileName =getRandomFileName();
			String extension = getFileExtension(thumbnail);
			makeDirPath(targetAddr);
			String relativeAddr = targetAddr + realFileName + extension;
			File dest = new File(PathUtil.getImgBasePath() + relativeAddr);
			try {
				Thumbnails.of(thumbnail.getInputStream()).size(200, 200).
				watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath+"shuiying.jpg")), 0.25f)
				.outputQuality(0.8f).toFile(dest);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return relativeAddr;
	}
	/**
	 * 生成随机文件名，当前年月日小时分钟秒钟+五位随机数
	 * @return
	 */
	private static String getRandomFileName() {
		int rannum=r.nextInt(89999)+10000;
		String nowTimeStr=sDateFormat.format(new Date());
			return nowTimeStr+rannum;
		}
	/**
	 * 创建目标路径所设涉及到的目录，即/home/work/xiagze/xxx.jpg
	 * 以上的三个文件夹自动创建
	 * @param targetAddr
	 */
	private static void makeDirPath(String targetAddr) {
		String realFileParentPath = PathUtil.getImgBasePath() + targetAddr;
		File dirPath = new File(realFileParentPath);
		if (!dirPath.exists()) {
			dirPath.mkdirs();
		}
	}
	/**
	 * 获取输入文件流的扩展名
	 * @param cFile
	 * @return
	 */
	private static String getFileExtension(CommonsMultipartFile cFile) {
		String originalFileName = cFile.getOriginalFilename();
		return originalFileName.substring(originalFileName.lastIndexOf("."));
	}
	public static void main(String[] args) throws IOException {
		Thumbnails.of(new File("E:/image/xiaohuangren.jpg")).size(200,200)
		.watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(basePath+"/shuiying.jpg")),
				0.25f).outputQuality(0.8f).toFile("E:/image/xiaohuangren6.jpg");
	}
}
